package com.cbx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.DBConnection;
import com.dao.ObjectDao;
import com.pro.bean.Visit_Log;


public class VisitDao extends ObjectDao{
	List<Visit_Log> selectVisitor(String crtcmp,String firstname,String username,String visittime)
	{
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		//Visit userMessage = new Visit();
		List<Visit_Log> Users=new ArrayList<Visit_Log>();
		List<String> ss=new ArrayList<String>();
		try {
			conn=DBConnection.getConnection();
			StringBuffer sql=new StringBuffer("select Visit_Log.visittime,info.cmp,info.firstname,unit.unitname,[users].username from info,visit_log,[users],unit where visit_log.cardid=info.cardid and visit_log.userid=[users].userid and [users].unitid=unit.unitid");
			if(crtcmp!=null && !crtcmp.equals(""))
			{
				ss.add(crtcmp);
				sql.append(" and info.cmp=?");
			}
			if(firstname!=null && !firstname.equals(""))
			{
				ss.add(firstname);
				sql.append(" and info.firstname=? ");
			}
			if(username!=null && !username.equals(""))
			{
				ss.add(username);
				sql.append(" and [users].username=? ");
			}
			if(visittime!=null && !visittime.equals(""))
			{
				ss.add(visittime);
				sql.append(" and Visit_Log.visittime=?");
			}
			//sql.append(" order by user.USERNAME");
			pstmt=conn.prepareStatement(sql.toString());

			for(int i=0;i<ss.size();i++)
			{
				pstmt.setString(i+1, ss.get(i));
			}
			rs=pstmt.executeQuery();
		
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			
		}
		return Users;
	}
}
